//
//  MakeOrderScreen.m
//  ServiceApp
//
//  Created by Training Camp on 12/17/14.
//  Copyright (c) 2014 TrainingCamp. All rights reserved.
//

#import "MakeOrderScreen.h"
#import "Table.h"
#import "TablesManager.h"

@interface MakeOrderScreen ()

@property Table *selectedTable;

@property (weak, nonatomic) IBOutlet UILabel *productLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

@end

static NSString *kMakeOrderUrl = @"http://78.90.20.186:8080/ProjectRestaurant/makeOrder";

@implementation MakeOrderScreen

- (void)viewDidLoad {
    [super viewDidLoad];
    self.productLabel.text = self.selectedProduct.name;
    self.priceLabel.text = [NSString stringWithFormat:@"%.2f lv.", self.selectedProduct.price];
    self.selectedTable = [[TablesManager sharedTables] selectedTable];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)makeOrderButton:(id)sender{
    NSNumber *tableId = [NSNumber numberWithInteger:self.selectedTable.tableNumber];
    NSNumber *productId = [NSNumber numberWithInteger:self.selectedProduct.productId];
    
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:tableId, @"tableId", productId, @"productId", nil];
    
    if ([NSJSONSerialization isValidJSONObject:dict]) {
        
        NSError *error = nil;
        NSData *result = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
        
        if (error == nil && result != nil) {
            NSData *response = [self jsonPostRequest:result];
            
            NSError* mistake = nil;
            NSDictionary* jsonDictionary = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&mistake];
            NSString *responseString = jsonDictionary[@"response"];
            if (mistake != nil) {
                NSLog(@"Error : %@", [mistake localizedDescription]);
            }
            else {
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Making Order" message:responseString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                if ([responseString isEqualToString:@"OK"]){
                    [self.navigationController popToViewController:self.navigationController.viewControllers[2] animated:YES];
                }
            }
        }
    }
}

-(id) jsonPostRequest:(NSData*) jsonRequestData{
    NSURL *url = [NSURL URLWithString:kMakeOrderUrl];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:60];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonRequestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:jsonRequestData];
    
    //send synch request
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    
    if (error == nil) {
        return result;
    }
    else{
        return nil;
    }
}


@end
